<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文本转语音</title>
    <style>
        body {
            background-color: black;
            color: white;
        }
    </style>
</head>
<body>
    <textarea id="text" rows="5" cols="50"></textarea><br>
    <button onclick="textToSpeech()">生成语音</button>
    <button onclick="downloadAudio()">下载</button>
    
    <script>
        var audioChunks = [];

        function textToSpeech() {
            // 获取输入的文本
            var text = document.getElementById("text").value;

            // 创建新的合成语音实例
            var synthesis = window.speechSynthesis;
            var utterance = new SpeechSynthesisUtterance();
            utterance.text = text;
            utterance.lang = "zh-CN";
            synthesis.speak(utterance);

            // 将合成语音转换为可下载的 Blob 对象
            utterance.addEventListener('boundary', function(event) {
                var chunk = event.target.voice || event.target;
                audioChunks.push(chunk);
            });
        }

        function downloadAudio() {
            var blob = new Blob(audioChunks, { type: 'audio/mp3' });
            var url = URL.createObjectURL(blob);
            var link = document.createElement('a');
            link.setAttribute('href', url);
            link.setAttribute('download', 'output.mp3');
            link.style.display = 'none';
            document.body.appendChild(link);
            link.click();
        }
    </script>
</body>
</html>
